package com.bio.dao.user.impl;

import java.util.List;

import org.hibernate.criterion.Restrictions;

import com.bio.dao.impl.HibernateDaoImpl;
import com.bio.dao.impl.HibernateQuery;
import com.bio.dao.user.UsertypeDao;
import com.bio.entity.user.User;
import com.bio.entity.user.Usertype;
import com.bio.utils.Pager;
import com.bio.utils.Pagination;

public class UsertypeDaoImpl extends HibernateDaoImpl<Usertype> implements UsertypeDao{

	@Override
	public Pager<Usertype> findPagerByCompanyId(Pagination pagination,
			Long companyId) {
		if(null == companyId){
			return new HibernateQuery<Usertype>(this.getSessionFactory(), 
				this.getSession(), Usertype.class).setPagination(pagination)
					                              .addWhere(Restrictions.isNull("companyId")).query();
		}else{
		    return new HibernateQuery<Usertype>(this.getSessionFactory(), 
				this.getSession(), Usertype.class).setPagination(pagination)
				                                  .addWhere(Restrictions.eq("companyId",companyId)).query();
		}
	}

	@Override
	public Usertype getByNameAndCompany(String name, Long companyId, Long id) {
		HibernateQuery<Usertype> hq = new HibernateQuery<Usertype>(this.getSessionFactory(),
				this.getSession(), Usertype.class).setPagination(null).addWhere(
						Restrictions.eq("name", name));
		if(null != companyId){
			hq.addWhere(Restrictions.eq("companyId",companyId));
		} else {
			hq.addWhere(Restrictions.isNull("companyId"));
		}
		if(null != id){
			hq.addWhere(Restrictions.not(Restrictions.eq("id", id)));
		}
		List<Usertype> types = hq.query().getResult();

		return types.size() > 0 ? types.get(0) : null;
	}

}
